package com.example.d1111_paste.slice;

import com.example.d1111_paste.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.miscservices.pasteboard.IPasteDataChangedListener;
import ohos.miscservices.pasteboard.PasteData;
import ohos.miscservices.pasteboard.SystemPasteboard;

import java.util.Timer;
import java.util.TimerTask;

public class MainAbilitySlice extends AbilitySlice {

    private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, "MainAbilitySlice.class");

    private TextField mTextFeild;
    private Button mCopy;
    private Button mPaste;
    private Text mText;

    private String string;

    private SystemPasteboard pasteboard;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        HiLog.info(LABEL_LOG, "onStart");


        mTextFeild = findComponentById(ResourceTable.Id_textfield);
        mCopy = findComponentById(ResourceTable.Id_copy_btn);
        mPaste = findComponentById(ResourceTable.Id_paste_btn);
        mText = findComponentById(ResourceTable.Id_text_helloworld);

        mCopy.setClickedListener(component -> onCopy());
        mPaste.setClickedListener(component -> onPaste());


        intit();

    }

    private void intit() {
        HiLog.info(LABEL_LOG, "intit");

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {

                //  初始化对象
                pasteboard = SystemPasteboard.getSystemPasteboard(getContext());

                //  添加一个系统剪切板的监听
                pasteboard.addPasteDataChangedListener(listener);
                PasteData data = pasteboard.getPasteData();

                //判断粘贴板是否有值
                if (pasteboard.hasPasteData()) {
                    String toString = data.getPrimaryText().toString();
                    if (toString.equals("hello paste")) {

                //            返回UI线程
                        getUITaskDispatcher().syncDispatch(new TimerTask() {
                            @Override
                            public void run() {
                                new ToastDialog(getContext()).setText("hello paste").show();
                            }
                        });
                    }

                }
                this.cancel();
            }

        }, 2000);


    }

    IPasteDataChangedListener listener = new IPasteDataChangedListener() {
        @Override
        public void onChanged() {

            PasteData data = pasteboard.getPasteData();

            // 当数据发生变化时，将值保存
            string = data.getPrimaryText().toString();
            new ToastDialog(getContext()).setText(string + "变动").show();
            HiLog.info(LABEL_LOG, string);

        }
    };

    private void onPaste() {
        PasteData data = pasteboard.getPasteData();
        boolean b = pasteboard.hasPasteData();
        String bb = String.valueOf(b);

        new ToastDialog(getContext()).setText(bb).show();
//        new ToastDialog(getContext()).setText(data.getPrimaryText().toString()).show();

        if (data == null && !b) {
            new ToastDialog(getContext()).setText("系统粘贴板为空").show();
            return;
        }
        PasteData.DataProperty property = data.getProperty();
        boolean a = property.hasMimeType(PasteData.MIMETYPE_TEXT_PLAIN);

        if (a && b && data != null && !data.equals("")) {

            new ToastDialog(getContext()).setText(data.getPrimaryText().toString()).show();

            for (int i = 0; i < data.getRecordCount(); i++) {
                PasteData.Record recordAt = data.getRecordAt(i);
                mText.setText(recordAt.getPlainText().toString());
                break;
            }
        }

    }

    private void onCopy() {
        if (pasteboard != null && mTextFeild != null && mTextFeild.getText() != null) {
            pasteboard.setPasteData(PasteData.creatPlainTextData(mTextFeild.getText().trim()));
        } else if (mTextFeild.getText() == null) {
            new ToastDialog(getContext()).setText("当前可选中内容为空,请在文本框输入内容").show();

        }

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (pasteboard != null) {
            pasteboard.removePasteDataChangedListener(listener);
            pasteboard.clear();
        }

    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
